var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
			hide:true
		}, {
			colkey : "cover",
			name : "封面图片",
			width : '90px',
			renderData : function(rowindex,data, rowdata, column) {
				if(data == "" ){
					return "--";
				}
				return "<a target='_blank' href='"+rootPath+data+"'><img style='width:60px;' src='"+rootPath+data+"'></a>";
			}
		},{
			colkey : "activityName",
			name : "活动名称"
		}, {
			colkey : "applyStartDate",
			name : "报名开始时间",
			width : '120px',
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm");
			}
		}, {
			colkey : "applyEndDate",
			name : "报名结束时间",
			width : '120px',
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm");
			}
		}, {
			colkey : "peoplenum",
			name : "报名人数",
			width : '70px'
		},{
			colkey : "peopleLimit",
			name : "招募人数",
			width : '70px'
		},{
			colkey : "status",
			name : "状态",
			width : '80px',
			renderData : function(rowindex,data, rowdata, column) {
				if(data == "0"){
					return "审核已过";
				}else if (data == "1"){
					return "待审核"
				}else{
					return "审核未通过"
				}
			}
		},{
			colkey : "isOfficial",
			name : "是否社区发起",
			width : '90px',
			renderData : function(rowindex,data, rowdata, column) {
				if(data == "Y"){
					return "是";
				}else{
					return "否"
				}
			}
		},
        {
            colkey : "integral",
            name : "积分",
            width : '50px'
        },
        {
        colkey : "createTime",
        name : "创建时间",
        width : '150px',
        renderData : function(rowindex,data, rowdata, column) {
            return new Date(data).format("yyyy-MM-dd hh:mm:ss");
        }
		} ],
		jsonUrl : rootPath + '/activity/findByPage.shtml',
		checkbox : true,
		serNumber : true
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addActivity").click("click", function() {
		addActivity();
	});
	$("#editActivity").click("click", function() {
		editActivity();
	});
	$("#delActivity").click("click", function() {
		delActivity();
	});
	$("#code").click("click", function() {
		code();
	});
	$("#signin").click("click", function() {
		signin();
	});
	$("#signup").click("click", function() {
		signup();
	});
	$("#voteResult").click("click", function() {
		voteResult();
	});
	$("#review").click("click", function() {
		review();
	});
	$("#pass").click("click", function() {
		pass();
	});
	$("#passFail").click("click", function() {
		passFail();
	});
	$("#integralEdit").click("click", function() {
        integralEdit();
	});
});
function checkstate(obj,id){
	var d = 'N';
	if(obj.checked){
		d = 'Y';
	}
	var url = rootPath + '/activity/updateState.shtml';
	var s = CommnUtil.ajax(url, {
		"activityFormMap.id" : id,
		"activityFormMap.status" : d
	}, "json");
	if (s == "success") {
		layer.msg('操作成功');
	} else {
		layer.msg('操作失败');
	}
}
function editActivity() {
	alert("编辑功能暂不开放");
	return ;
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	var url = rootPath + '/activity/editUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "编辑",
		type : 1,
		area : [ "900px", "100%" ],
		content : CommnUtil.ajax(url)
	});
}
function addActivity() {
	var url =rootPath + '/activity/addUI.shtml';
	pageii = layer.open({
		title : "新增",
		type : 1,
		area : [ "900px", "100%" ],
		content : CommnUtil.ajax(url)
	});
}
function delActivity() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/activity/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
function pass() {
	var cbox = grid.selectRow();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	cbox = cbox[0];
	if(cbox.status != "1"){
		layer.alert("该活动已审核");
		return ;
	}
	layer.confirm('是否通过？', function(index) {
		var url = rootPath + '/activity/updateStatus.shtml';
		var s = CommnUtil.ajax(url, {
			"communityActivityFormMap.id" : cbox.id,
			"communityActivityFormMap.status" : 0
		}, "json");
		if (s == "success") {
			layer.msg('操作成功');
			grid.loadData();
		} else {
			layer.msg('操作失败');
		}
	});
}
function passFail() {
	var cbox = grid.selectRow();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	cbox = cbox[0];
	if(cbox.status != "1"){
		layer.alert("该活动已审核");
		return ;
	}
	layer.confirm('是否不通过？', function(index) {
		var url = rootPath + '/activity/updateStatus.shtml';
		var s = CommnUtil.ajax(url, {
			"communityActivityFormMap.id" : cbox.id,
			"communityActivityFormMap.status" : 2
		}, "json");
		if (s == "success") {
			layer.msg('操作成功');
			grid.loadData();
		} else {
			layer.msg('操作失败');
		}
	});
}
function integralEdit() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }

    var url = rootPath + '/activity/integralUI.shtml?id=' + cbox;
    pageii = layer.open({
        title : "积分配置",
        type : 1,
        area : [ "500px", "50%" ],
        content : CommnUtil.ajax(url)
    });
}
function code() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}

	var url = rootPath + '/activity/codeUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "二维码",
		type : 1,
		area : [ "500px", "80%" ],
		content : CommnUtil.ajax(url)
	});

}
function signin() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}

	var url = rootPath + '/activity/signinUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "签到人员信息",
		type : 1,
		area : [ "500px", "80%" ],
		content : CommnUtil.ajax(url)
	});

}
function signup() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}

	var url = rootPath + '/activity/signupUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "报名人员信息",
		type : 1,
		area : [ "500px", "80%" ],
		content : CommnUtil.ajax(url)
	});


}
function voteResult() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}

	var url = rootPath + '/activity/voteResultUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "投票结果统计",
		type : 1,
		area : [ "500px", "80%" ],
		content : CommnUtil.ajax(url)
	});

}
function review() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}

	var url = rootPath + '/activity/reviewUI.shtml?id=' + cbox;
	pageii = layer.open({
		title : "活动结果汇报",
		type : 1,
		area : [ "900px", "100%" ],
		content : CommnUtil.ajax(url)
	});

}